﻿using MongoDB.Driver;
using DAL.Mongo.Interface;

namespace DAL.Mongo.Configuration
{
    public class MongoConfigurationManager : IConfigurationManager
    {
        protected readonly IConfiguration _config;
        protected readonly MongoClient _client;
        protected readonly MongoServer _server;
        protected readonly MongoDatabase _database;

        public MongoConfigurationManager(IConfiguration config)
        {
            _config = config;
            _client = new MongoClient(_config.DefaultConnectionString);
            _server = _client.GetServer();
            _database = _server.GetDatabase(_config.DefaultDatabaseName);
        }

        public virtual MongoCollection<TEntity> GetCollectionFrom<TEntity>()
        {
            return _database.GetCollection<TEntity>(GetEntityName<TEntity>());
        }

        protected string GetEntityName<TEntity>()
        {
            return typeof(TEntity).Name;
        }
    }
}
